package com.worksystem.Controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.worksystem.Common.Common;
import com.worksystem.Service.LoginService;
import com.worksystem.Service.TaskService;
import com.worksystem.entity.Comment;
import com.worksystem.entity.Task;
import com.worksystem.entity.User;

@Controller
@RequestMapping("dashboard")
public class DashboardController extends Common {
	
	@Resource
	private TaskService taskDao;
	
	@Resource
	private LoginService UserDao;
	//进入默认主页
	@RequestMapping()
	public ModelAndView defaultmethod(HttpServletRequest request, HttpServletResponse response)  {
		ModelAndView mv=new ModelAndView();
		HttpSession session=requst.getSession();
		if(session.getAttribute("onlineuser")!=null){
			mv.setViewName("/dashboard/dashboard");
			User user=(User) session.getAttribute("onlineuser");
			List<Task> tasks=taskDao.getUserTasklist(user,0,10);
			mv.addObject("tasks", tasks);
			mv.addObject("user",user);
		}
		else{
			mv.setViewName("user/login");
		}
		return mv;
	}
	
	@RequestMapping(value="/ajax/list",method = { RequestMethod.POST, RequestMethod.GET },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public Object getTaskByJSON(Integer uid){
		if(uid!=null){
			User user=UserDao.getUserByUid(uid);
			List<Task> tasks=taskDao.getUserTasklist(user, 1,10);
			JSONArray ja=processorParameter(tasks, new String[]{"loginPwd"});
			return ja.toString();
		}
		else{
			HttpSession session=requst.getSession();
			if(session.getAttribute("onlineuser")!=null){
				User user=(User) session.getAttribute("onlineuser");
				List<Task> tasks=taskDao.getUserTasklist(user, 1,10);
				JSONArray ja=processorParameter(tasks, new String[]{"loginPwd"});
				return ja.toString();
			}
			else{
				return "false";
			}
		}
		
		
	}
	
	@RequestMapping(value="/ajax/taskuserlist",method = { RequestMethod.POST, RequestMethod.GET },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public Object getTaskuserlistByJSON(Integer tid){
		if(tid!=null){
			User user=taskDao.getUserByTaskId(tid);
			JSONObject ob=new JSONObject(user);
			return ob.toString();
		}
		else{
			return "false";
		}
	}
	
	@RequestMapping(value="/ajax/taskdetail",method = { RequestMethod.POST, RequestMethod.GET },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public Object getTaskinfomation(Integer tid){
		if(tid==null){
			return false;
		}
		Task task=taskDao.getTaskById(tid);
		if(task!=null){
			List<Comment> comments=taskDao.getCommentsByTaskId(tid);
			JSONArray taskjson=processorParameter(task,null);
			JSONArray commentjson=processorParameter(comments,new String[]{"task"});
			JSONObject result=new JSONObject();
			result.put("task", taskjson);
			result.put("comments", commentjson);
			return result.toString();
		}
		else{
			return "false";
		}
		
		
		
	}
	
	//任务详情页面
	@RequestMapping(value="/task/{id}")
	public ModelAndView taskContent(@PathVariable("id") Integer id){
		System.out.println("获取当前任务详情，当前任务Id"+id);
		ModelAndView mv=new ModelAndView();
		HttpSession session=requst.getSession();
		if(session.getAttribute("onlineuser")!=null){
			//注入对象
			mv=injectDefaultModel(mv);
			Task task=taskDao.getTaskById(id);
			if(task!=null){
				List<Comment> comments=taskDao.getCommentsByTaskId(id);
				mv.addObject("comments", comments);
				mv.addObject("task", task);
			}
			mv.setViewName("/dashboard/task");
		}
		else{
			mv.setViewName("user/login");
		}
		return mv;
	}
	
	
	//提交评论
	@RequestMapping(value="/task/docomment",method = { RequestMethod.POST, RequestMethod.GET },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public Object postComment(String editorValue,String tid,Integer uid){
		
		HttpSession session=requst.getSession();
		if(session.getAttribute("onlineuser")!=null || uid!=null){
			System.out.println(editorValue);
			Task task=new Task();
			task.setTid(Integer.valueOf(tid));
			Comment comment=new Comment();
			comment.setBody(editorValue);
			comment.setTask(task);
			comment.setTime(new Date());
			//两者取其一
			if(session.getAttribute("onlineuser")!=null){
				User user=(User) session.getAttribute("onlineuser");
				comment.setUser(user);
			}
			else if(uid!=null){
				User user = new User();
				user.setUid(uid);
				comment.setUser(user);
			}
			try {
				taskDao.saveComment(comment);
				return "true";
			} catch (Exception e) {
				return e.getMessage();
			}
			
		}
		//如果没有登陆过的用户提交要出错
		else{
			return "false";
		}
	}
}
